<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Manage City</title>
<link rel="stylesheet" type="text/css" href="css/FinalProjectCSS.css"/>
<!-- <link type="text/javascript" href="js/MasterJS.js"/> -->
<script type="text/javascript" src="table.js"></script>
<script type="text/javascript" src="js/MasterJS.js"></script>
<script type="text/javascript">
	var updateId = "";
	
	function loadUpdate(id){
		if(updateId != ""){
			cancelUpdate(updateId);
		}		
		
		var editPlace = document.getElementById("editText" + id);
		var inputPlace = document.getElementById("editTextInput" + id);
		var text = editPlace.innerHTML;

		//create input text
		var textNode = document.createElement("input");		
		textNode.setAttribute("type", "text");		
		textNode.setAttribute("value", text);
		textNode.setAttribute("style", "width:100%;");
		
		//set visible
		editPlace.setAttribute("style", "display:none;");
		inputPlace.setAttribute("style", "display:'';");		
		inputPlace.appendChild(textNode);	
		
		loadUpdateButton(id);		
		updateId = id;
	}
	
	function submitUpdate(task, id){	
		if(task == "insertCity"){
			var insertText = document.getElementById("insertCityNameText");
			document.forms[0].elements["schoolModel.cityName"].value = insertText.value;
			//validasi
			if (!validateForm(document.forms[0])) {
				return;
			}
		}else{
			if(task == "updateCity"){
				var inputPlace = document.getElementById("editTextInput" + id);
				var textNode = inputPlace.getElementsByTagName("input")[0];
				document.forms[0].elements["schoolModel.cityName"].value = textNode.value;
				//validasi
				if (!validateForm(document.forms[0])) {
					return;
				}
			}
				document.forms[0].elements["schoolModel.cityId"].value = id;
		}
		flyToPage(task);
	}
	
	function cancelUpdate(id){
		if(id != "0"){
			var editPlace = document.getElementById("editText" + id);
			var inputPlace = document.getElementById("editTextInput" + id);
			
			//set visible
			editPlace.setAttribute("style", "display:'';");
			inputPlace.setAttribute("style", "display:none;");
			
			//reset edit place
			inputPlace.innerHTML = "";
			
			cancelUpdateButton(id);
		}else{
			hideInsertField();
		}		
		updateId = "";
	}
	
	function searchByParam() {
		document.forms[0].elements['paramCity'].value = document.getElementById('searchCity').value;
		document.forms[0].submit();
	}
	
	function hideInsertField(){
		document.getElementById("insertCityNameText").value = "";
		hideInsertForm();
	}
	
	function showFieldInsert(){
		if(updateId != ""){
			cancelUpdate(updateId);
		}
		updateId = "0";
		showInsertForm();
	}
	
	function setDisplay() {
		document.getElementById('searchCity').value = document.forms[0].elements['paramCity'].value;
		document.getElementById('searchCity').focus(); 
	}
	
</script>
</head>

<html:javascript formName="schoolForm" dynamicJavascript="true" staticJavascript="true" method="validateForm" page="0"/>
<%
	if(session.getAttribute("result") != null)
	{
		String message = session.getAttribute("result").toString();
		%>
			<script type="text/javascript">
				window.alert('<%=message%>');
			</script>
		<%
		session.removeAttribute("result");
	}	
%> 

<body onload="javascript:hideInsertField();setDisplay();" onkeydown="javascript:disableF5(event, '');">
	<center>
		<table width="100%" class="bodyTable boxShadow">
		
			<!-- Header -->
			<tr>
				<td colspan="2"><%@include file="/include/Header.jsp"%></td>
			</tr>
		
			<!-- Content -->
			<tr>
				<td rowspan="2" width="200px" class="navigationBox"><%@include file="/include/Navigation.jsp"%></td>
				<td align="left" class="headerBox blueBackground">Setting > Manage City</td>
			</tr>
			<tr>
				<td align="center"  class="contentStyle"><br>
					
					<!-- Insert Code Here -->
					<html:form action="/Master" method="post">
						<html:hidden property="task"/>
						<html:hidden property="schoolModel.cityId"/>
						<html:hidden property="schoolModel.cityName"/>
						<html:hidden property="paramCity"/>
						<table border="0" width="80%" align="center" class="boxShadow  example table-autosort table-autofilter table-autopage:10 table-stripeclass:alternate table-page-number:t1page table-page-count:t1pages table-filtered-rowcount:t1filtercount table-rowcount:t1allcount" id="t1">			
							<thead>
							<tr class="headerBox formHeaderBackground">
<!-- 								<th align="center" class="table-filterable table-sortable:default" style="padding:5px 7px;">City ID</th> -->
								<th colspan="2" class="formListStyle filterable table-sortable:default">City Name</th>
								<th></th>
							</tr>
							<tr>
								<th colspan="2" class="formListStyle" align="center"><input type="text" class="textStretch" id="searchCity" onkeyup="javascript:searchByParam();" /></th>
							</tr>
							</thead>
							<tbody>
							<logic:notEmpty name="schoolForm" property="cityList">
								<logic:iterate id="listData" name="schoolForm" property="cityList">
									<tr class="formListBackground">
<%-- 										<td class="formListStyle"><bean:write name="listData" property="cityId"></bean:write></td> --%>
										<td class="formListStyle leftAlign" colspan="2">
											<div id='editText<bean:write name="listData" property="cityId"/>'><bean:write name="listData" property="cityName"/></div>
											<div id='editTextInput<bean:write name="listData" property="cityId"/>' style="display:none;"></div>
										</td>
										<td align="center" class="formListStyle">
											<span id='editButton<bean:write name="listData" property="cityId"/>'>
												<img src="images/editButton.png" title="edit" style="cursor: pointer;" onclick="javascript:loadUpdate('<bean:write name="listData" property="cityId"/>');"/>
												&nbsp;
												<img src="images/deleteButton.png" title="delete" style="cursor: pointer;" onclick="javascript:submitUpdate('deleteCity', '<bean:write name="listData" property="cityId"/>');"/>
											</span>
											<span id='saveButton<bean:write name="listData" property="cityId"/>' style="display:none;">
												<img src="images/saveButton.png" title="save" style="cursor: pointer;" onclick="javascript:submitUpdate('updateCity', '<bean:write name="listData" property="cityId"/>');"/>
												&nbsp;
												<img src="images/cancelButton.png" title="cancel" style="cursor: pointer;" onclick="javascript:cancelUpdate('<bean:write name="listData" property="cityId"/>');"/>
											</span>
										</td>
									</tr>
								</logic:iterate>
							</logic:notEmpty>
							<logic:empty name="schoolForm" property="cityList">
								<tr class="formListBackground">
									<td colspan="3" class="formListStyle" align="center">Data is not found</td>
								</tr>
							</logic:empty>
							</tbody>
							<tfoot>
							<tr class="headerBox formHeaderBackground">
								<td width="80px" class="table-page:previous formListStyle leftAlign" style="cursor:pointer;">&lt;&lt; Previous</td>
								<td style="text-align:center; vertical-align: middle;">Page <span id="t1page"></span>&nbsp;of <span id="t1pages"></span></td>
								<td width="55px" align="right" class="table-page:next formListStyle rightAlign" style="cursor:pointer;">Next &gt;&gt;</td>
							</tr>	
							<tr id="insertRow" class="formListStyle">
<!-- 								<td></td> -->
								<td colspan="2" align="center" class="formListStyle"><input type="text" id="insertCityNameText" style="width:100%;"></td>
<!-- 								<td width="100px"><input type="button" id="insertBtn" class="buttonStyle" value="Add" onclick="javascript:flyToPage('insertCity');"></td> -->
<!-- 								<td width="100px"><input type="button" id="cancelBtn" class="buttonStyle" value="Cancel" onclick="javascript:hideInsertField();"></td> -->
								<td align="center" class="formListStyle">
									<span>
										<img src="images/saveButton.png" title="submit" style="cursor: pointer;" onclick="javascript:submitUpdate('insertCity', '');"/>
										&nbsp;
										<img src="images/cancelButton.png" title="cancel" style="cursor: pointer;" onclick="javascript:hideInsertField();"/>
									</span>
								</td>
							</tr>							
							<tr id="insertButton">
								<td colspan="3" title="add new" align="center" class="greyBackground">						
									<div class="insertButtonGridStyle" onclick="javascript:showFieldInsert();">
										<table>
											<tr>
												<td class="formListStyle"><img src="images/addButton.png"></td>
												<td class="formListStyle"><b>Add New City</b></td>
											</tr>
										</table>
									</div>									
								</td>
							</tr>	
							<tr>
								<td colspan="3" align="center" class="headerBox formHeaderBackground"><span id="t1filtercount"></span>&nbsp;of <span id="t1allcount"></span>&nbsp;rows match filter(s)</td>
							</tr>
							</tfoot>
						</table>
					</html:form>
				
					<br>
				</td>
			</tr>
			
			<!-- Footer -->
			<tr>
				<td align="center" colspan="2" class="headerBox blueBackground">&copy; OME. 2014. All Rights Reserved.</td>
			</tr>
		</table>
	</center>
</body>
</html>
